package com.example.Lezedata.ui.activity.person;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.example.Lezedata.R;
import com.example.Lezedata.dataManager.GBUserManager;
import com.example.Lezedata.dataManager.LoginManager;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.BankCard.BankCardVc;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.ui.activity.setting.PinSettingVc;
import com.example.Lezedata.ui.fragment.commonfragment.AlertFragment;
import com.example.Lezedata.ui.fragment.commonfragment.TransactionPinView;
import com.example.Lezedata.util.GBUtil;
import com.google.gson.Gson;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import bid.Bid;
import common.Common;

public class WithDrawalVc extends GBBaseVc {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_with_drawal_vc;
    }

    @Override
    public CharSequence title() {
        return "Withdraw";
    }

    public TextView balance;
    public TextView selectBank;
    public EditText accountNumber;
    public TextView coins;
    public TextView amount;
    public TextView tips;

    private String bankId;
    private String BankName;
    private ArrayList<String> bankJson;

    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        findView();
    }

    void findView(){
        balance = findViewById(R.id.balance);
        selectBank = findViewById(R.id.selectBank);
        accountNumber = findViewById(R.id.accountNumber);
        coins = findViewById(R.id.coins);
        amount = findViewById(R.id.amount);
        tips = findViewById(R.id.tips);
        findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });

        selectBank.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectBank();
//                jumpActivity(BankCardVc.class);
            }
        });
    }

    void setPinPsd(){
        AlertFragment fragment = new AlertFragment();
        fragment.text = "Please set your PIN";
        fragment.okListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpActivity(PinSettingVc.class);
            }
        };
        fragment.show(getSupportFragmentManager(),"alert");
    }

    void submit(){
        boolean hasPinPsd = GBUserManager.instance().hasPIN;
        if (!hasPinPsd){
            setPinPsd();
            return;
        }
        TransactionPinView ms = TransactionPinView.newInstance();
        ms.inputListener = new TransactionPinView.InputListener() {
            @Override
            public void InputComplete(String text) throws NoSuchAlgorithmException {
                Withdraw(text);
            }
        };
        ms.show(getSupportFragmentManager(),"TransactionPinView");
    }

    void Withdraw(String pwd) throws NoSuchAlgorithmException {
        Bid.WithdrawRequest request = Bid.WithdrawRequest.newBuilder()
                .setAmount(0)
                .setTransPw(GBUtil.getSHA256(pwd))
                .build();
        GBNetRequest.stubUser().withdraw(request, ServerResponseObserver.observer(new ServerResponseObserver.requestListener() {
            @Override
            public void complete(Object value, GBError error) {
                if (error == null){

                }
            }
        }));
    }

    public ArrayList<String> getBankJson() {
        if (bankJson == null){
            String Json = GBUtil.getJson("province.json",mContext);
            Gson gson = new Gson();
            ArrayList<String> list = gson.fromJson(Json, ArrayList.class);
            bankJson = list;
        }
        return bankJson;
    }

    void selectBank(){
        ArrayList<String> province_Json = getBankJson();
        final ArrayList<String> options1Items = new ArrayList<>();
        options1Items.addAll(province_Json);

        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
                Log.d("TAG", "onOptionsSelect: ");
                BankName = options1Items.get(options1);
            }
        }).build();
        Button cancel = (Button)pvOptions.findViewById(R.id.btnCancel);
        Button submit = (Button)pvOptions.findViewById(R.id.btnSubmit);

        cancel.setBackgroundColor(Color.TRANSPARENT);
        submit.setBackgroundColor(Color.TRANSPARENT);

        cancel.setTextColor(getResources().getColor(R.color.app_color_main));
        submit.setTextColor(getResources().getColor(R.color.app_color_main));
        pvOptions.setPicker(options1Items);
        pvOptions.show();
    }
}